from PIL import Image
def crop_img_percentage(box,image_path):

    try:
        image = Image.open(image_path)
    except:
        image = Image.open("error.jpg")
        print('file format error,open default image>>>')
        # pipe.send(img+"不存在！")

    w_pixel=image.size[0]
    #print(image.size)
    h_pixel=image.size[1]
    x_left_pixel = w_pixel * box[0]
    y_left_pixel = h_pixel * box[1]
    crop_w_pixel = w_pixel * box[2]
    crop_h_pixel = h_pixel * box[3]
    #print("x_left_pixel, y_left_pixel,x_left_pixel+ crop_w_pixel,y_left_pixel+ crop_h_pixe",x_left_pixel, y_left_pixel,x_left_pixel+ crop_w_pixel,y_left_pixel+ crop_h_pixel)
    crop_img = image.crop((x_left_pixel, y_left_pixel,x_left_pixel+ crop_w_pixel,y_left_pixel+ crop_h_pixel))
    #return crop_img
    crop_img.save("tempt_img.jpg")
    return image.size

def get_percentage_list(box_dic):
    box_list=[]
    box_list.append(box_dic['leftX']/1000)
    box_list.append(box_dic['leftY']/1000)
    box_list.append(box_dic['xLength']/1000)
    box_list.append(box_dic['yLength']/1000)
    return box_list


def check_image_valid(pathfile):
    bValid = True
    try:
        Image.open(pathfile).verify()
    except:
        bValid = False
    return bValid
if __name__ == '__main__':
    image=Image.open("/home/leslie/ftpd/192.168.168.35/192.168.168.35_01_20200603155043241_TIMING.jpg")